package com.restaurant.application.level;

import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.restaurant.application.level.support.AbstractLevel;
import com.restaurant.application.level.support.LevelData;
import com.restaurant.application.levelset.LevelSet;

public class LevelImpl extends AbstractLevel {
    @Override
    public ViewUpdate save(LevelData save, int[] seq) {
        if (getData().getId() != null) {
            save.withId(getData().getId()).withCreateTime(getData().getCreateTime());
        } else {
            save.withId(SnowFlake.findUniqueId()).withCreateTime(DateTimeUtils.getCurrentMillisecond());
        }
        save.withModifyTime(DateTimeUtils.getCurrentMillisecond());
        setData(save);
        getSingleton(LevelSet.class).addLevel(getId());
        return returnPopFromContainer(getId(), false);
    }

    @Override
    public ViewUpdate cancel(int[] seq) {
        return returnPopFromContainer(getId(), false);
    }

    @Override
    public View getFullView() {
        PageForm pageForm = newFormShell(getData().duplicate()).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }

    @Override
    public View getViewLead(ViewSpec spec) {
        return null;
    }

    @Override
    public LevelData findData() {
        return getData().duplicate();
    }

    @Override
    public boolean checkScore(long score) {
        return getData().getScore() <= score;
    }

    private FormShell newFormShell(LevelData levelData) {

        FormShell formShell = new FormShell("会员等级", 3, 3);

        formShell.source(levelData, DataModes.VALIDATE);

        formShell.at(0, 0, "等级")
                .getEntry(LevelData.FN_MEMBERLEVEL, levelData.getMemberLevel() == null ? "" : String.valueOf(levelData.getMemberLevel()))
                .placeHolder("")
                .simpleEntry(EntryTypes.LONG)
                .getContent().setEditable(true);

        formShell.at(1, 0, "等级名")
                .getEntry(LevelData.FN_MEMBERNAME, levelData.getMemberName())
                .placeHolder("")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(true);

        formShell.at(2, 0, "所需积分")
                .getEntry(LevelData.FN_SCORE, levelData.getScore() == null ? "" : String.valueOf(levelData.getScore()))
                .placeHolder("")
                .simpleEntry(EntryTypes.LONG)
                .getContent().setEditable(true);

        formShell.at(3, 0, "保存")
                .buttonField(getPath(CMD_SAVE), "", true);

        formShell.at(3, 1, "取消")
                .buttonField(getPath(CMD_CANCEL), "", false);

        return formShell;
    }
}
